package com.ceba.test.modules.regist.dao;

import abc.ID;
import abc.data.DataRow;
import abc.data.DataTable;
import abc.data.Sql;
import abc.data.SqlSessionManager;
import abc.data.store.IStore;
import abc.data.store.StoreManager;
import abc.object.IObject;
import abc.object.XObject;
import com.ceba.base.config.CommonConfig;
import com.ceba.cebautils.StringUtil;
import abc.data.dataSource.SqlDataSource;
import com.ceba.test.modules.regist.entity.EnterpriseRegistLic;

import java.util.Map;

public class NewEnterpriseRegistDao {

    public DataTable queryEnterpriseRegistLicAll(String key, Map<String, Object> dic,String type) {
        SqlDataSource dataSource = new SqlDataSource();
        Sql sql = null;
        if (!StringUtil.isEmptyString(key)) {
            sql = new Sql("SELECT e.ID,e.ENTERPRISE_NAME,el.ALLOW_LOGIN_DATE," +
                    "el.ALLOW_SIGN_NUM,el.FORMAL_USE_TIME,el.CREATE_TIME,el.SYSTEM_TYPE,el.ALL_SIGN_NUM,el.SIGNED_COUNT, " +
                    "el.VERSION,el.CUSTOMER_GROUP,el.ONLINE_WORKER FROM lic_enterprise e  " +
                    "LEFT JOIN g_enterprise_lic el ON el.FK_ENTERPISE_ID = e.ID " +
                    "WHERE e.ENTERPRISE_NAME like '%" + key + "%' order by e.CREATE_TIME desc");
        } else {
            sql = new Sql("SELECT e.ID,e.ENTERPRISE_NAME,el.ALLOW_LOGIN_DATE," +
                    "el.ALLOW_SIGN_NUM,el.FORMAL_USE_TIME,el.CREATE_TIME,el.SYSTEM_TYPE,el.ALL_SIGN_NUM,el.SIGNED_COUNT, " +
                    "el.VERSION,el.CUSTOMER_GROUP,el.ONLINE_WORKER FROM lic_enterprise e  " +
                    "LEFT JOIN g_enterprise_lic el ON el.FK_ENTERPISE_ID = e.ID " +
                    "WHERE (e.ENTERPRISE_NAME is not null) OR (e.ENTERPRISE_NAME like '%" + key + "%') order by e.CREATE_TIME desc");
        }
        DataTable dt;
        if(type.equals("page")){
            //分页查询
            dataSource.getSelectCommands().add(sql);
            dt = dataSource.query(0, dic);
        }else{
            dt = SqlSessionManager.getDataTable(sql);
        }
        return dt;
    }

    /**
     * 根據客戶信息表id刪除記錄
     *
     * @param enterpriseId
     * @return
     */
    public boolean getDeleteNewEnterpriseRegist(String enterpriseId) {
        Sql sql = new Sql("delete from g_enterprise_lic where FK_ENTERPISE_ID='" + enterpriseId + "'");
        return SqlSessionManager.execute(sql);
    }


    /**
     *根据客户获取信息Id 获取license 对象
     *
     */

    public EnterpriseRegistLic  getEnterpriseRegistLic(String enterpriseId) throws  Exception {
        Sql sql = new Sql("select * from g_enterprise_lic where FK_ENTERPISE_ID='" + enterpriseId + "'");
        DataTable dt = SqlSessionManager.getDataTable(sql);
        if (dt != null && dt.getRowCount() > 0) {
            EnterpriseRegistLic enterpriseRegistLic = new EnterpriseRegistLic();
            for( DataRow dr:dt){
                copyDataTableToEnterpriseRegistLic(dr,enterpriseRegistLic);
            }
          return  enterpriseRegistLic;
        }
        return null;
    }

    public DataTable getEnterpriseRegistByName(String enterpriseName)throws Exception {
        Sql sql = new Sql("SELECT * FROM lic_enterprise WHERE ENTERPRISE_NAME=?");
        sql.addParameter(enterpriseName);
        DataTable dt = SqlSessionManager.getDataTable(sql);
        return dt;
    }


    /**
     *
     * 修改lic
     * @param lic
     */

    public  boolean updateEnterpriseRegistLic (EnterpriseRegistLic lic) throws  Exception{
        IStore store = StoreManager.create("g_enterprise_lic");
        XObject object = new XObject("ID",lic.getId());
        setObjectValue( object, lic);
//        object.setUpdateUser();
        object.setCreateTime();
        if(store.update(object)>0){
            return true;
        }
        return false;
    }

    /**
     *
     * 增加lic
     * @param lic
     */

    public  boolean addEnterpriseRegistLic (EnterpriseRegistLic lic) throws  Exception{
        IStore store = StoreManager.create("g_enterprise_lic");
        IObject object = new XObject("g_enterprise_lic");
        object.put("ID", lic.getId());
        setObjectValue( object, lic);
        object.setCreateTime();
//        object.setCreateUser();
        if(store.insert(object)>0){
            return true;
        }
        return false;
    }





    public  void setObjectValue(IObject object ,EnterpriseRegistLic lic){


        object.put("FK_ENTERPISE_ID",lic.getFkEnterpiseId());
        object.put("ALLOW_LOGIN_DATE",lic.getAllowLoginDate());
        object.put("ALLOW_SIGN_NUM",CommonConfig.getEncode(lic.getAllowSignNum()));
        object.put("ALL_SIGN_NUM",CommonConfig.getEncode(lic.getAllSignNum()));
        object.put("SIGNED_COUNT",CommonConfig.getEncode(lic.getSignedCount()));
        object.put("REMOTE_NEW_SIGN_COUNT",CommonConfig.getEncode(lic.getRemoteNewSignCount()));
        object.put("ACTIVE_TIMES",lic.getActiveTimes());
        object.put("ONLINE_WORKER",lic.getOnlineWorker());
        object.put("SIGN_VALIDITY",lic.getSignValidity());
        object.put("FORMAL_USE_TIME",lic.getFormalUseTime());
        object.put("SYSTEM_TYPE",CommonConfig.getEncode(lic.getSystemType()));
        object.put("SYSTEM_FEE_TYPE",lic.getSystemFeeType());
        object.put("USE_VERSION",lic.getUseVersion());
        object.put("CUSTOMER_GROUP",lic.getCustomerGroup());
        object.put("SHCA_KEY_FLAG",StringUtil.isEmptyString(lic.getShcaKeyFlag())==true?"0" :lic.getShcaKeyFlag());
        object.put("SVN_VERSION",lic.getSvnVersion());
        object.put("VERSION",lic.getVersion());
        object.put("SMT_SYSTEM_DEFAULTS_LANGUAGE",lic.getSmtSystemDefaultsLanguage());
        object.put("SYSTEM_DEFAULTS_LANGUAGE",lic.getSystemDefaultsLanguage());
        object.put("SYSTEM_PRODUCT_NAME",lic.getSystemProductName());
        object.put("IDS_SERVER_PORT",lic.getIdsServerPort());
        object.put("NETTY_SERVER_IP",lic.getNettyServerIp());
        object.put("NETTY_SERVER_PORT",lic.getNettyServerPort());
        object.put("WATERMARK_OVERCONTENT_FLAG",lic.getWatermarkSvercontentFlag());
        object.put("LOGIN_TYPE",lic.getLoginType());
        object.put("FILE_STORAGE_FALG",lic.getFileStorageFalg());
        object.put("PDF_CREATE_MODE",lic.getPdfCreateMode());
        object.put("PATTER_LOGO_FLAG",lic.getPatterLogoFlag());
        object.put("ZIP_DOWN_MAX_SIZE",lic.getZipDownMaxSize());
        object.put("MAX_SIZE",lic.getMaxSize());
        object.put("PDF_TRANSFORM_URL",lic.getPdfTransformUrl());
        object.put("PDF_TRANSFORM_SOCKET_PORT",lic.getPdfTransformSocketPort());
        object.put("GROUP_MANAGER_FLAG",lic.getGroupManagerFlag());
        object.put("SE_GROUP_FLAG",lic.getSeGroupFlag());
        object.put("SE_DELETE_SINGLE_FLAG",lic.getSeDeleteSingleFlag());
        object.put("MAX_PAGE",lic.getMaxPage());
        object.put("PUBLIC_KEY_FLAG",lic.getPublicKeyFlag());
        object.put("BATCH_SIGN",lic.getBatchSign());
        object.put("SEND_MAIL_MAX_SIZE",lic.getSendMailMaxSize());
        object.put("MERGE_MAX_COUNT",lic.getMergeMaxCount());
        object.put("QIFENG_FLAG",StringUtil.isEmptyString(lic.getQifengFlag())==true?"false" :lic.getQifengFlag());
        object.put("REPORTMERGE_FLAG",lic.getReportmergeFlag());
        object.put("SIGN_FILE_MAX",lic.getSignFileMax());
        object.put("BE_KEY_FLAG",lic.getBeKeyFlag());
        object.put("SHOW_DIR_MANAGE",lic.getShowDirManage());
        object.put("LICENSE_IMPORT_TIME",lic.getLicenseImportTime());
        object.put("SERVER_SIGN_FLOW",lic.getServerSignFlow());
        object.put("AUTOMATIC_SIGNATURE_FLAG",lic.getAutomaticSignatureFlag());

    }






    public  void copyDataTableToEnterpriseRegistLic (DataRow dr,EnterpriseRegistLic enterpriseRegistLic) throws  Exception{
        enterpriseRegistLic.setId(dr.getString("ID"));
        enterpriseRegistLic.setFkEnterpiseId(dr.getString("FK_ENTERPISE_ID"));
        enterpriseRegistLic.setAllowLoginDate(dr.getString("ALLOW_LOGIN_DATE"));
        enterpriseRegistLic.setAllowSignNum(CommonConfig.getDecode(dr.getString("ALLOW_SIGN_NUM")));
        enterpriseRegistLic.setAllSignNum(CommonConfig.getDecode(dr.getString("ALL_SIGN_NUM")));
        enterpriseRegistLic.setSignedCount(CommonConfig.getDecode(dr.getString("SIGNED_COUNT")));
        enterpriseRegistLic.setRemoteNewSignCount(CommonConfig.getDecode(dr.getString("REMOTE_NEW_SIGN_COUNT")));
        enterpriseRegistLic.setActiveTimes(dr.getString("ACTIVE_TIMES"));
        enterpriseRegistLic.setOnlineWorker(dr.getString("ONLINE_WORKER"));
        enterpriseRegistLic.setSignValidity(dr.getString("SIGN_VALIDITY"));
        enterpriseRegistLic.setFormalUseTime(dr.getString("FORMAL_USE_TIME"));
        enterpriseRegistLic.setSystemType(CommonConfig.getDecode(dr.getString("SYSTEM_TYPE")));
        enterpriseRegistLic.setSystemFeeType(dr.getString("SYSTEM_FEE_TYPE"));
        enterpriseRegistLic.setUseVersion(dr.getString("USE_VERSION"));
        enterpriseRegistLic.setCustomerGroup(dr.getString("CUSTOMER_GROUP"));
        enterpriseRegistLic.setShcaKeyFlag(dr.getString("SHCA_KEY_FLAG"));
        enterpriseRegistLic.setSvnVersion(dr.getString("SVN_VERSION"));
        enterpriseRegistLic.setVersion(dr.getString("VERSION"));
        enterpriseRegistLic.setSmtSystemDefaultsLanguage(dr.getString("SMT_SYSTEM_DEFAULTS_LANGUAGE"));
        enterpriseRegistLic.setSystemDefaultsLanguage(dr.getString("SYSTEM_DEFAULTS_LANGUAGE"));
        enterpriseRegistLic.setSystemProductName(dr.getString("SYSTEM_PRODUCT_NAME"));
        enterpriseRegistLic.setIdsServerPort(dr.getString("IDS_SERVER_PORT"));
        enterpriseRegistLic.setNettyServerIp(dr.getString("NETTY_SERVER_IP"));
        enterpriseRegistLic.setNettyServerPort(dr.getString("NETTY_SERVER_PORT"));
        enterpriseRegistLic.setWatermarkSvercontentFlag(dr.getString("WATERMARK_OVERCONTENT_FLAG"));
        enterpriseRegistLic.setLoginType(dr.getString("LOGIN_TYPE"));
        enterpriseRegistLic.setFileStorageFalg(dr.getString("FILE_STORAGE_FALG"));
        enterpriseRegistLic.setPdfCreateMode(dr.getString("PDF_CREATE_MODE"));
        enterpriseRegistLic.setPatterLogoFlag(dr.getString("PATTER_LOGO_FLAG"));
        enterpriseRegistLic.setZipDownMaxSize(dr.getString("ZIP_DOWN_MAX_SIZE"));
        enterpriseRegistLic.setMaxSize(dr.getString("MAX_SIZE"));
        enterpriseRegistLic.setPdfTransformUrl(dr.getString("PDF_TRANSFORM_URL"));
        enterpriseRegistLic.setPdfTransformSocketPort(dr.getString("PDF_TRANSFORM_SOCKET_PORT"));
        enterpriseRegistLic.setGroupManagerFlag(dr.getString("GROUP_MANAGER_FLAG"));
        enterpriseRegistLic.setSeGroupFlag(dr.getString("SE_GROUP_FLAG"));
        enterpriseRegistLic.setSeDeleteSingleFlag(dr.getString("SE_DELETE_SINGLE_FLAG"));
        enterpriseRegistLic.setMaxPage(dr.getString("MAX_PAGE"));
        enterpriseRegistLic.setPublicKeyFlag(dr.getString("PUBLIC_KEY_FLAG"));
        enterpriseRegistLic.setBatchSign(dr.getString("BATCH_SIGN"));
        enterpriseRegistLic.setSendMailMaxSize(dr.getString("SEND_MAIL_MAX_SIZE"));
        enterpriseRegistLic.setMergeMaxCount(dr.getString("MERGE_MAX_COUNT"));
        enterpriseRegistLic.setQifengFlag(dr.getString("QIFENG_FLAG"));
        enterpriseRegistLic.setReportmergeFlag(dr.getString("REPORTMERGE_FLAG"));
        enterpriseRegistLic.setSignFileMax(dr.getString("SIGN_FILE_MAX"));
        enterpriseRegistLic.setBeKeyFlag(dr.getString("BE_KEY_FLAG"));
        enterpriseRegistLic.setShowDirManage(dr.getString("SHOW_DIR_MANAGE"));
        enterpriseRegistLic.setLicenseImportTime(dr.getString("LICENSE_IMPORT_TIME"));
    }




}
